package com.cn.por.util.study;

/**
 * 测试基本的线程
 */

public class ThreadTest {

    private int a = 0;

    public static void main(String[] args) {


        /**
         * 查看线程状态
         */

        ThreadTest threadTest = new ThreadTest();

        Thread thread1 = new Thread(()->{
            threadTest.doSomething(1);
        },"thread1");

        Thread thread2 = new Thread(()->{
            try {
                Thread.sleep(20000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            threadTest.doSomething(2);
        },"thread2");

        // 创建一个线程并定义其任务
        Thread myThread = new Thread(() -> {
            try {

                // 线程内部休眠 1 秒（进入 TIMED_WAITING 状态）
                Thread.sleep(1000);
                threadTest.doSomething(0);
                Integer i=0;
                while(i<1000000000){
                    i++;
                }


            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "MyThread"); // 给线程命名，方便识别

        // 1. 查看线程启动前的状态（NEW）
        System.out.println("启动前状态：" + myThread.getState());

        // 启动线程
        myThread.start();
        thread1.start();
        thread2.start();
        // 2. 查看线程运行中的状态（可能是 RUNNABLE 或 TIMED_WAITING）
        try {
            Thread.sleep(500); // 等待一小段时间，让线程进入休眠状态
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println("休眠中状态：" + myThread.getState());

        try {
            Thread.sleep(600); // 等待一小段时间，让线程进入休眠状态
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println("等待监视器锁状态：" + myThread.getState());

        try {
            Thread.sleep(10000); // 等待一小段时间，让线程进入休眠状态
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("休眠状态：" + myThread.getState());


        try {
            Thread.sleep(10000); // 等待一小段时间，让线程进入休眠状态
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("无限期等待状态：" + myThread.getState());


        try {
            Thread.sleep(10000); // 等待一小段时间，让线程进入休眠状态
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("运行时状态：" + myThread.getState());

        // 3. 等待线程执行完毕
        try {
            myThread.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        // 4. 查看线程结束后的状态（TERMINATED）
        System.out.println("结束后状态：" + myThread.getState());
    }

    public void doSomething(int state){
        synchronized (this){
            a++;
            try {
                Thread.sleep(10000);
                if (state==0){
                    wait();
                }
                if (state==2){
                    notifyAll();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
